// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------
// Modification history:
// feature: Unified Transport Abstraction Layer Unified Transport Abstraction Layer
// feature: support multiple sending based on topic
// ------------------------------------------------------------------

#ifndef INCLUDE_EDDS_RTPS_NETWORK_SENDERRESOURCE_H_
#define INCLUDE_EDDS_RTPS_NETWORK_SENDERRESOURCE_H_

#include <functional>
#include <vector>
#include <chrono>
#include <asio/error_code.hpp>
#include <deps/common/Types.h>
#include <ertps/utils/LocatorAlias.h>
#include <deps/common/LocatorList.hpp>
#include <deps/common/LocatorsIterator.hpp>

using namespace vbsutil::xmlparser;
using AsyncSendHandler = std::function<void(const asio::error_code&, size_t)>;

namespace vbs {
namespace transport {

/**
 * RAII object that encapsulates the Send operation over one chanel in an unknown transport.
 * A Sender resource is always univocally associated to a transport channel; the
 * act of constructing a Sender Resource opens the channel and its destruction
 * closes it.
 * @ingroup NETWORK_MODULE
 */
class SenderResource {
 public:
    /**
     * Sends to a destination locator, through the channel managed by this resource.
     * @param data Raw data slice to be sent.
     * @param dataLength Length of the data to be sent. Will be used as a boundary for
     * the previous parameter.
     * @param destination_locators_begin destination endpoint Locators iterator begin.
     * @param destination_locators_end destination endpoint Locators iterator end.
     * @param max_blocking_time_point If transport supports it then it will use it as maximum
     * blocking time.
     * @return Success of the send operation.
     */
    bool send(octet* data, uint32_t dataLength, LocatorsIterator* destination_locators_begin,
              LocatorsIterator* destination_locators_end) {
        bool returned_value = false;

        if (send_lambda_) {
            returned_value = send_lambda_(data, dataLength, destination_locators_begin, destination_locators_end);
        }

        return returned_value;
    }

    /**
     * Sends to a destination locator, through the channel managed by this resource.
     * @param data Raw data slice to be sent.
     * @param dataLength Length of the data to be sent. Will be used as a boundary for
     * the previous parameter.
     * @param destination_locators_begin destination endpoint Locators iterator begin.
     * @param destination_locators_end destination endpoint Locators iterator end.
     * @param max_blocking_time_point If transport supports it then it will use it as maximum
     * blocking time.
     * @return Success of the send operation.
     */
    bool async_send(octet* data, uint32_t dataLength, LocatorsIterator* destination_locators_begin,
                    LocatorsIterator* destination_locators_end, AsyncSendHandler handler) {
        bool returned_value = false;

        if (async_send_lambda_) {
            returned_value = async_send_lambda_(data, dataLength, destination_locators_begin, destination_locators_end,
                                                std::move(handler));
        }

        return returned_value;
    }

    /**
     * Resources can only be transfered through move semantics. Copy, assignment, and
     * construction outside of the factory are forbidden.
     */
    SenderResource(SenderResource&& rValueResource) : transport_kind_(0), order_(0) {
        clean_up.swap(rValueResource.clean_up);
        send_lambda_.swap(rValueResource.send_lambda_);
        async_send_lambda_.swap(rValueResource.async_send_lambda_);
    }

    virtual ~SenderResource() = default;

    int32_t kind() const { return transport_kind_; }

    int32_t order() const { return order_; }
    /**
     * Add locators representing the local endpoints managed by this sender resource.
     *
     * @param [in,out] locators  List where locators will be added.
     */
    virtual void add_locators_to_list(LocatorList& locators) const { (void)locators; }
    virtual bool check_ip_address(const Locator_t& locator) const {
        (void)locator;
        return false;
    }
    virtual bool is_whitelisted() { return false; }

 protected:
    SenderResource(int32_t transport_kind) : transport_kind_(transport_kind), order_(0), async_send_lambda_(nullptr) {}

    int32_t transport_kind_;
    int32_t order_;

    std::function<void()> clean_up;
    std::function<bool(octet*, uint32_t, LocatorsIterator* destination_locators_begin,
                       LocatorsIterator* destination_locators_end)>
        send_lambda_;
    std::function<bool(Locator_t& remote_locator)> find_remote_send_lambda_;
    std::function<bool(octet*, uint32_t, LocatorsIterator* destination_locators_begin,
                       LocatorsIterator* destination_locators_end, AsyncSendHandler handler)>
        async_send_lambda_;

 private:
    SenderResource() = delete;
    SenderResource(const SenderResource&) = delete;
    SenderResource& operator=(const SenderResource&) = delete;
};

}  // namespace transport
}  // namespace vbs

#endif  // INCLUDE_EDDS_RTPS_NETWORK_SENDERRESOURCE_H_
